import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/loginPage.vue')
  },
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/homePage.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//路由拦截器
router.beforeEach((to, from, next) => {
  //检查有没有token，没有token返回登录页面
  const token = localStorage.getItem('token')
  if (!token && to.path !== '/login') {
    next('/login')
  }
  next()
})

export default router
